Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat: fetch LMS_USER_ID from LMS #584

Merged
merged 3 commits into from
Feb 12, 2024
Merged

feat: fetch LMS_USER_ID from LMS #584

merged 3 commits into from
Feb 12, 2024

Conversation

sameenfatima78
Copy link
Member

@sameenfatima78 sameenfatima78 commented Feb 2, 2024

Description

We cannot include user_id claim in JWT for external API requests. We get hit by "user_id not found in your jwt" error while calling bulk-licesne-enroll POST endpoint. To fix this, we are now fetching user_id from LMS in case if it is not found in JWT.

Link to the associated ticket: https://2u-internal.atlassian.net/browse/ENT-8351

Testing considerations

  • Include instructions for any required manual tests, and any manual testing that has
    already been performed.
  • Include unit and a11y tests as appropriate
  • Consider performance issues.
  • Check that Database migrations are backwards-compatible

Post-review

Squash commits into discrete sets of changes

@sameenfatima78 sameenfatima78 changed the title feat: fetch LMS_USER_ID from LMS WIP --- feat: fetch LMS_USER_ID from LMS Feb 2, 2024
@sameenfatima78 sameenfatima78 changed the title WIP --- feat: fetch LMS_USER_ID from LMS [DRAFT] WIP --- feat: fetch LMS_USER_ID from LMS Feb 2, 2024
Comment on lines 1191 to 1202
""""
Another approach would be to override this property at child class level.
@cached_property
def lms_user_id(self):
try:
return utils.get_key_from_jwt(self.decoded_jwt, 'user_id')
except ParseError:
lms_client = LMSApiClient()
user_id = lms_client.fetch_lms_user_id(self.request.user.email)
return user_id
"""

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Still need to check if this is the right way to override a property. Stackoverflow answers suggests to override the property setter method instead. The changes worked locally though. https://stackoverflow.com/questions/3336767/overriding-inherited-properties-getters-and-setters-in-python

def lms_user_id(self):
return utils.get_key_from_jwt(self.decoded_jwt, 'user_id')
try:
return utils.get_key_from_jwt(self.decoded_jwt, 'user_id')
Copy link
Member

@adamstankiewicz adamstankiewicz Feb 2, 2024

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

[suggestion] It may be worth following the pattern of integrating the lms_user_id as a model field on the core User model, similar to how enterprise-access is set up.

The benefit of this is that the user id is persisted in the database for each User record, which is created whenever an authenticated API request is made to the service.

For example, the EDX_DRF_EXTENSIONS setting can be modified to include lms_user_id (example source) and the lms_user_id field can be added to the core User model (example source).

Copy link
Member

@adamstankiewicz adamstankiewicz Feb 2, 2024

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Hmm, that said, I suppose this would still assume a JWT cookie with a user_id claim...

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

You could use the pattern Adam described above and do something like this:

  1. Try to lookup the lms_user_id by email from the model in this service.
  2. If it's not present, then go fetch from the LMS.
  3. Populate the model record with the fetched lms_user_id.

@zamanafzal
Copy link
Member

Hi @adamstankiewicz @iloveagent57 Thank you for the feedback. These are great suggestions. Can we incorporate these changes in a follow-up PR as this change is needed on an urgent basis?

@zamanafzal zamanafzal changed the title [DRAFT] WIP --- feat: fetch LMS_USER_ID from LMS feat: fetch LMS_USER_ID from LMS Feb 12, 2024
Copy link
Contributor

@iloveagent57 iloveagent57 left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I think this is fine for now. I did some investigating and I think the “right” way to fix the problem of the lms user id not being in the JWT is:

  1. modify the base edx oauth client to pass in user_id in the scope list in the access token request payload, and also
  2. Ensure there’s an ApplicationAccess record in the LMS for the license-manager service worker that says user_id is allowed to be requested when fetching the token.

I can open a ticket and route to you guys.

@iloveagent57
Copy link
Contributor

ah I mispoke slightly - the JWT we care about here is the API user's JWT, not the service worker. So we'd have to bake the ApplicationAccess creation into our flow for how those users get API credentials, maybe.

@zamanafzal
Copy link
Member

@iloveagent57 Thank you for digging into it. That will be a great help if you can open a ticket with all these details.

@zamanafzal zamanafzal merged commit 54b8202 into master Feb 12, 2024
6 of 7 checks passed
@zamanafzal zamanafzal deleted the sameen/ENT-8351 branch February 12, 2024 17:00
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

4 participants